#!@BASH@
# -*- mode: sh -*-
# create -v4 logs for DWGs, you can override -v?
srcdir="@srcdir@"
top_builddir="@top_builddir@"

make -s -j4 -C src
make -s -j4 -C programs dwgread
@DSYMUTIL@ programs/dwgread
v=-v4
# dummy to please shellcheck
test -z "$v" && echo $top_builddir
ob="$b"
log=

for d in "$@"; do
    # we do use bash
    case $d in
        -v*) v="$d"
             ;;
        -l*) log="$(cut "$d" -c2-)"
             ;;
        *)
        if [ ! -f "$d" ]; then
            if [ -f "$srcdir/test/test-data/$d" ]
            then
                d="$srcdir/test/test-data/$d";
            else
                if [ -f "$srcdir/test/test-data/$d.dwg" ]
                then
                    d="$srcdir/test/test-data/$d.dwg"
                fi
            fi
        fi
        xd=$(dirname "$d")
        dir=_$(basename "$xd")
        if [ -z "$log" ]; then
            case "$d" in
                *to/*)
                    x1=$(dirname "$xd")
                    dir=_$(basename "$xd")_$(basename "$x1")
                    ;;
                *test-old/*)
                    x1=$(dirname "$xd")
                    dir=_$(basename "$xd")_$(basename "$x1")
                    ;;
            esac
        fi
        if [ "$dir" = "_test-data" ]; then dir=""; fi
        if [ "$dir" = "_td" ]; then dir=""; fi
        if [ "$dir" = "_." ]; then dir=""; fi
        b=$(basename "$d" .dwg)
        [ -z "$log" ] && log="$b$dir.log"
        echo "@TIMEOUT_10@ @LTEXEC@ programs/dwgread $v $d 2>$log"
        # (sleep 12s; killall dwgread 2>/dev/null) &
        @TIMEOUT_10@ @LTEXEC@ programs/dwgread "$v" "$d" 2>"$log"
        tail -n2 "$log"
        log=
        ;;
    esac
done
v=
b="$ob"
